package leecode

func maxAreaOfIslandLCR(grid [][]int) int {
	var dfs func(grid [][]int, row, column int) int
	dfs = func(grid [][]int, row, column int) int {
		if !isAreaLCR(grid, row, column) {
			return 0
		}
		if grid[row][column] != 1 {
			return 0
		}
		grid[row][column] = 2
		top := dfs(grid, row-1, column)
		down := dfs(grid, row+1, column)
		left := dfs(grid, row, column-1)
		right := dfs(grid, row, column+1)

		return top + down + left + right + 1
	}
	var result int
	for i := 0; i < len(grid); i++ {
		for j := 0; j < len(grid[0]); j++ {
			if grid[i][j] == 1 {
				result = max(result, dfs(grid, i, j))
			}
		}
	}
	return result

}

func isAreaLCR(grid [][]int, row, column int) bool {
	return 0 <= row && row < len(grid) && 0 <= column && column < len(grid[0])
}
